<template>
  <div class="app-container">
    <!--  查询  -->
    <el-card>
      <el-form :inline="true" :model="formInline" class="formHeight">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="参数名称">
              <el-input
                v-model="formInline.label_name"
                size="mini"
                clearable
                placeholder="请输入参数名称"
                @change="FormChange"
              />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="参数标识">
              <el-input
                v-model="formInline.column_name"
                size="mini"
                clearable
                placeholder="请输入参数标识"
                @change="FormChange"
              />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="状态">
              <el-select v-model="formInline.status" placeholder="请选择状态" @change="FormChange" size="mini">
                <el-option label="请选择状态" value="" />
                <el-option label="启用" value="1" />
                <el-option label="禁用" value="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" @click="clearSearch" size="mini">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
    </el-card>

    <!--   dialog 区域 -->
    <el-dialog :visible.sync="excelDialog">
      <UploadExcel :before-upload="beforeUpload" :on-success="success" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="excelDialog = false" size="mini">取消</el-button>
        <el-button type="primary" @click="excelDialog = false" size="mini">确定</el-button>
      </span>
    </el-dialog>

    <!--  列表  -->
    <el-card class="mt20">

      <div slot="header" class="clearfix">
        <span>参数类型</span>
        <div class="CardButton">
          <el-button type="primary" size="mini" @click="addOrEdit(1)">新增</el-button>
          <el-button type="success" size="mini" @click="exportExcelTem()">
            <svg-icon icon-class="excel" />
            导出
          </el-button>
          <el-button type="danger" size="mini" @click="importExcelTem()">
            <svg-icon icon-class="excel" />
            导入
          </el-button>
        </div>
      </div>

      <el-table
        :data="tableData"
        style="width: 100%;"
        :default-sort="{prop: formInline.sort, order: formInline.order}"
        @sort-change="tableSortChange"
      >
        <el-table-column
          property="label_name"
          label="参数名称"
          align="center"
        />
        <el-table-column
          property="column_name"
          label="参数标识"
          align="center"
        />
        <el-table-column
          label="参数类型"
          align="center"
        >
          <template slot-scope="scope">
            <div>{{ scope.row.type.type_name }}</div>
          </template>
        </el-table-column>
        <el-table-column
          property="status"
          label="状态"
          align="center"
        >
          <template slot-scope="scope">
            <div>
              <el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
              <el-tag v-if="scope.row.status === 0" type="danger">禁止</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              type="warning"
              size="mini"
              @click="addOrEdit(2, scope.row.id)"
            >
              修改
            </el-button>
            <el-button
              type="danger"
              size="mini"
              @click="del(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--   分页   -->
<!--      <el-pagination-->
<!--        style="float: right;"-->
<!--        class="paginationPage"-->
<!--        :current-page="formInline.offset"-->
<!--        layout="total, sizes, prev, pager, next, jumper"-->
<!--        :total="total"-->
<!--        :page-sizes="[10,20,30,40,50,60,100, 200, 300, 400]"-->
<!--        :page-size="10"-->
<!--        @size-change="handleSizeChange"-->
<!--        @current-change="handleCurrentChange"-->
<!--      />-->

      <pagination style="float: right;" v-show="total>0" :total="total" :page.sync="formInline.offset" :limit.sync="formInline.limit" @pagination="getList" />
    </el-card>
    <div v-if="detailFlag">
      <Detail
        :id="detailId"
        :dialog-visible="detailFlag"
        :title="detailTitle"
        @close="close"
        @confirm="confirm"
      />
    </div>
  </div>
</template>

<script>
import UploadExcel from '@/components/UploadExcel'
import { dataList, deteleData, exprotData, importData } from '@/api/dataType'
import Detail from './components/detail'

export default {
  name: 'datas.data',
  components: {
    Detail,
    UploadExcel
  },
  data() {
    return {
      detailTitle: '新增',
      detailId: 0,
      detailFlag: false,
      tableData: [],
      total: 0,
      // 搜索表单
      formInline: {
        sort: 'created_at',
        offset: 0,
        limit: 10,
        order: 'ascending',
        label_name: '',
        status: ''
      },
      excelDialog: false,
      errorList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 清空表单
    clearSearch() {
      this.formInline = {
        sort: 'created_at',
        offset: 0,
        limit: 10,
        order: 'ascending',
        label_name: '',
        status: ''
      }
      this.search()
    },
    // 状态修改查询
    FormChange() {
      this.search()
    },

    //  获取数据参数列表
    getList() {
      dataList(this.formInline).then(res => {
        this.tableData = res.data.list
        this.total = res.data.total
      })
    },
    /**
     * @description: 排序
     * @author: 书童
     */
    tableSortChange(column) {
      this.formInline.order = column.order ? column.order : 'descending'
      this.formInline.sort = column.prop
      this.formInline.offset = 0
      this.getList()
    },
    /**
     * @description: 删除数据
     * @author: 书童
     * @param: id
     */
    del(id) {
      this.$confirm('是否要删除此条数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deteleData({ id }).then(res => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getList()
        })
      })
    },
    /**
     * @description: 查询
     * @author: 书童
     */
    search() {
      this.formInline.offset = 0
      this.getList()
    },
    addOrEdit(num, id) {
      if (num === 1) {
        this.detailTitle = '新增'
        this.detailFlag = true
      } else {
        this.detailTitle = '编辑'
        this.detailId = id
        this.detailFlag = true
      }
    },
    //  关闭弹窗组件
    close() {
      this.detailFlag = false
      this.detailId = ''
    },
    // 弹窗返回
    confirm() {
      this.close()
      this.getList()
    },
    handleSizeChange(val) {
      this.formInline.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.formInline.offset = val
      this.getList()
    },
    // 导出
    exportExcelTem() {
      exprotData(this.formInline).then(res => {
        this.$message({
          message: '导出成功',
          type: 'success'
        })
        window.location = res.data.url
      })
    },
    importExcelTem() {
      this.excelDialog = true
    },
    beforeUpload(data) {
      return data
    },
    success(result) {
      importData({ data: result.results }).then(res => {
        this.$message({
          message: '导入成功',
          type: 'success'
        })
        this.getList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.formHeight{
  height: 40px;
}
</style>
